define(
	[
		'require',
		'./totalranking_ctrl'
	],
	function(require){
		function actionFormatter(value, row, index) {
	        return '<div class="form-column-summary" style="text-align:left;width:300px;word-break:break-all;overflow:hidden;" title="'+value+'">'+APM.util.escape(value)+'</div>';
	    }
	    var saveData = [],
	    	numData = [];

		var Controller = require('./totalranking_ctrl').extend({
			formTitle: "错误SQL排行",
			arrayData: Ember.A(['system/success/rate']),

			formTopArray: Ember.A([{"name":"Top10","id":"10"},{"name":"Top50","id":"50"},{"name":"Top100","id":"100"}]),

			columns: [
			  [{
			    "field": "resourceId",
			    "title": "SQL",
			    width:300,
			    formatter:actionFormatter,
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "system/throughput/total",
			    "title": "总次数",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "system/error/count",
			    "title": "总错误数",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "system/success/rate",
			    "title": "成功率%",
			    "colspan": 1,
			    "rowspan": 1
			  }],
			],
			
			formTopValueChange: Ember.observer("formTopValue",function(){
				if(this.get("formTopValue") == 10){
					this.set("chartsWidth",20);
				}else if(this.get("formTopValue") == 50){
					this.set("chartsWidth",4);
				}else if(this.get("formTopValue") == 100){
					this.set("chartsWidth",2);
				}
				this.reportformData();
			}),
			applicationValueChange: Ember.observer("applicationValue",function(){
				this.reportformData();
				
			}),
			/*
				向服务端请求业务系统列表
			*/
			requireApplications: function(){
				var ctrl = this;
				return this.store.ajax("GET", "applications")
				.then(function(response){
					var applications = Ember.A(response.applications);
					ctrl.set("applications",applications);
					ctrl.set("applicationValue",applications[0].id);
					return applications;
				});
			},

			// //根据数据来填充表格里的内容
			// fillTableContent: function(node,index){

			// 	$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
			// 	var tableTr = $(".table-left-container .tbody .table-left-tr").eq(index);
			// 	$(".table-left-container .tbody .table-left-tr").css("height","auto");
			// 	if(!node.dimensionId){
			// 		return;
			// 	}

			// 	var num = ((node.dimensionId.length)*50);
			// 	if(index == 0){
			// 		num = num - 1;
			// 	}

			// 	saveData.push(num);
				
			// 	if(node.resourceId){
			// 		$("<div class='table-left-td '><div class='word-break' style='display:table-cell;vertical-align:middle;'><div class='form-column-summary'>"+node.resourceId+"</div></div></div>").appendTo(tableTr);
			// 	}else{
			// 		$("<div class='table-left-td'><div class='word-break' style='text-align:right;'>-</div></div>").appendTo(tableTr);
			// 	}
			// 	if(node["system/throughput/total"]){
			// 		$("<div class='table-left-td'><div class='word-break' style='text-align:right;'>"+node["system/throughput/total"]+"</div></div>").appendTo(tableTr);
			// 	}else{
			// 		$("<div class='table-left-td' ><div class='word-break' style='text-align:right;'>-</div></div>").appendTo(tableTr);
			// 	}
			// 	if(node["system/error/count"]){
			// 		$("<div class='table-left-td'><div class='word-break' style='text-align:right;'>"+node["system/error/count"]+"</div></div>").appendTo(tableTr);
			// 	}else{
			// 		$("<div class='table-left-td'><div class='word-break'  style='text-align:right;'>-</div></div>").appendTo(tableTr);
			// 	}
			// 	if(node["system/success/rate"]){
			// 		$("<div class='table-left-td'><div class='word-break' style='text-align:right;'>"+node["system/success/rate"]+"</div></div>").appendTo(tableTr);
			// 	}else{
			// 		$("<div class='table-left-td' ><div class='word-break' style='text-align:right;'>0</div></div>").appendTo(tableTr);
			// 	}
			// 	$(".table-left-container .tbody .table-left-td").css("width","25%");


			// 	$(".table-left-container .tbody .table-left-tr").eq(index).find(".word-break").css({"height":num,"line-height":num+'px'});
			// 	$(".table-left-container .tbody .table-left-tr").eq(index).find(".word-break").on('click',function(){
			// 		for(var i=0;i<saveData.length-1;i++){
			// 			$(".fixed-table-container tbody tr").find("td").eq(i).css({"height":(saveData[i]/(numData[i]))});
			// 			$(".table-left-container .tbody .table-left-tr").eq(i).find(".word-break").css({"height":saveData[i],"line-height":saveData[i]+'px'});
			// 		}
			// 		$(".table-left-container .tbody .table-left-tr").find(".form-set-width").removeClass('form-set-width').addClass("form-column-summary");
			// 		$(this).find(".form-column-summary").removeClass('form-column-summary').addClass("form-set-width");
			// 		if($(this).find(".form-set-width").height() > num){
			// 			var value = $(this).find(".form-set-width").height();
			// 			if(value<=36){
			// 				value = 36;
			// 			}
			// 			if(index == 0){
			// 				value = value-1;
			// 			}

			// 			$(".table-left-container .tbody .table-left-tr").eq(index).find(".word-break").css({"height":value,"line-height":value+'px'});

			// 			$(".fixed-table-container tbody tr").eq(index).find("td").css({"height":value});
			// 		};
			// 	});
			// },

			// //根据数据插入表格头部
			// fillTableHeader: function(){
			// 	$(".table-left-container .thead").find("div").remove();
			// 	$(".table-left-container .thead").append("<div class='clearfix table-left-tr'></div>");
			// 	var tableTr = $(".table-left-container .thead .table-left-tr");

			// 	$("<div class='table-left-td pull-left'>SQL</div>").appendTo(tableTr);
			// 	$("<div class='table-left-td pull-left'>总次数</div>").appendTo(tableTr);
			// 	$("<div class='table-left-td pull-left'>总错误数</div>").appendTo(tableTr);
			// 	$("<div class='table-left-td pull-left'>成功率%</div>").appendTo(tableTr);
			// 	$(".table-left-container .thead .table-left-td").css("width","25%");
			// 	$(".table-left-container .thead").css({"height":"40px","line-height":"40px"});
			// },

			/*
				向服务端请求表格数据
			*/
			reportformData: function(){

				if(!this.get("formTopValue")){
					return;
				}

				if(!this.get("applicationValue")){
					return;
				}
				numData = [];
				var ctrl = this;
				ctrl.set("tipTitle","");
				var params = {
					"startDate": +this.get("startDate"),
					"endDate": +this.get("endDate"),
					"appId": ctrl.get("applicationValue"),
					"top": ctrl.get("formTopValue")
				}
				var newArray = [],
					newObject;

				ctrl.set("tipTitle","");

				return this.store.ajax("GET", "report/sql/error",params)
				.then(function(response){
					var result = response.result;

					//图表数据数据
					var chartsData = [],
						arr = [];
					
					if(result[0]){
						ctrl.fillTableHeader();
						$(".table-left-container .tbody").find("div").remove();
						// result.forEach(function(node,index){
							
						// 	ctrl.fillTableContent(node,index);
						// 	if(node["dimensionId"]){
						// 		numData.push(node["dimensionId"].length);
						// 		node.dimensionId.forEach(function(app){

						// 			newArray.push(Ember.merge(app,{"resourceId":node.resourceId}));
						// 		});
						// 	}
							
						// });
					}else{
						ctrl.set("tipTitle","这段时间内暂无数据");
						// $(".table-left-container .tbody").find("div").remove();
						// $(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
						// $(".table-left-container .tbody .table-left-tr").css("height","49px");
					
						// ctrl.fillTableHeader();
						chartsData = [];
					}

					Ember.A(result).forEach(function(node,index){
						arr.push(node);
					});
					ctrl.set("chartsData",result);
					ctrl.set("groupData",arr);
					setTimeout(function(){
						$(".bootstrap-table .table > tbody > tr").click(function(){
							$(".bootstrap-table .table > tbody > tr").find("td div").addClass("form-column-summary");
							$(this).find("td div").removeClass("form-column-summary");
						});
					},0)
					// setTimeout(function(){
					// 	var width = $(".table-wrapper").width();
					// 	$(".fixed-table-container tbody .no-records-found td").css({"textAlign":"left"}).html('');
					// 	$(".fixed-table-container tbody .no-records-found td").append($("<div style='position:absolute;left:"+(-((width/2-(width*0.3))+24))+"px;top:54px;'>没有数据</div>"))
					// },0);
				});
			},
			formTopValue:10,
			url:"report/sql/error/download",
		});

		APM.FormErrorSQLController = Controller;
		return Controller;
	}
);